package com.pet.server.ext.action.nuomi;

import net.sf.json.JSONObject;
import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.util.JsonUtil;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;
import com.pet.server.ext.util.nuomi.NuomiClient;

public class Shopinfo extends ActionBase{

	public String shop_id;
	public Shopinfo(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {
		shop_id=params.getFieldValue("shop_id");
	    if(StringUtil.isEmpty(shop_id)){
	    	throw ApiError.Type.INVALID_PARAM.toException();
	    }
	
	}
	
	@Override
	public Result invokeService() throws ApiError {
		JSONObject dataJson = NuomiClient.dealDetail(shop_id);
		Integer errno = JsonUtil.getValue(dataJson,"errno", Integer.class);
		JSONObject json = new JSONObject();
	    if(errno==0){
	    	JSONObject so = dataJson.getJSONObject("deal");//("");
    		json.put("deal_id", so.getString("deal_id"));//团单id,商户描述
    		json.put("image", so.getString("image"));//图片
    		json.put("tiny_image", so.getString("tiny_image"));//小图
    		json.put("mid_image", so.getString("mid_image"));//中图
    		json.put("title", so.getString("title"));//商户标题
    		json.put("min_title", so.getString("min_title"));
    		json.put("description", so.getString("description"));//商户描述
    		json.put("is_reservation_required", so.getBoolean("is_reservation_required"));
    		json.put("market_price",   getPrice(so.getString("market_price")));//市场价 (单位:分)
    		json.put("current_price", getPrice(so.getString("current_price")));//当前团购价
    		json.put("promotion_price", getPrice(so.getString("promotion_price")));//促销价
    		json.put("sale_num", so.getInt("sale_num"));//已售数
    		json.put("deal_murl", so.getString("deal_murl"));//url
    		json.put("publish_time", so.getInt("publish_time"));//团单发布时间,
    		json.put("purchase_deadline", so.getInt("purchase_deadline"));//团单购买最后期限
	    	json.put("buy_contents", so.getString("buy_contents"));//购买内容
    		json.put("consumer_tips", so.getString("consumer_tips"));//消费提示
    		json.put("buy_details", so.getString("buy_details"));//购买详情
    		json.put("consumer_tips", so.getString("consumer_tips"));//消费提升
    		json.put("buy_details", so.getString("buy_details"));//
    		json.put("shop_description", so.getString("shop_description"));
    		json.put("coupon_start_time", so.getInt("coupon_start_time"));//券有效开始时间	
    		json.put("coupon_end_time", so.getInt("coupon_end_time"));//券有效截止时间
        }else{
        	throw ApiError.Type.INVALID_PARAM.toException();
        }
    	Result result = new Result();
    	result.put("data", json);
	    return result;
	}

	public Float getPrice(String fen){
		return StringUtil.isEmpty(fen)||"null".equals(fen)?null:NumberUtil.getInteger(fen)/100f;
	}
	
	/**
    public List<ProductInfo> coverToProduct(List<XmlData> list){
    	List<ProductInfo> l = new ArrayList<ProductInfo>();
    	for(XmlData d:list){
    		ProductInfo p = new ProductInfo(-1, d.deal_name, d.deal_img, d.deal_wap_url, "001003", "", "", "", "", "", null, d.value, d.price);
    		if(!StringUtil.isEmpty(latitude,longitude)){
    			double distance = DistanceEstimator.route(latitude, longitude, "", d.deal_lat, d.deal_lng, "");
    			p.distance=distance;
    		}
    		
    		l.add(p);
    	}
    	return l;
    }
    */
	
}
